//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package org.activiti.engine.impl.persistence.entity.data.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.activiti.engine.impl.JobQueryImpl;
import org.activiti.engine.impl.Page;
import org.activiti.engine.impl.cfg.ProcessEngineConfigurationImpl;
import org.activiti.engine.impl.db.ListQueryParameterObject;
import org.activiti.engine.impl.persistence.CachedEntityMatcher;
import org.activiti.engine.impl.persistence.entity.JobEntity;
import org.activiti.engine.impl.persistence.entity.JobEntityImpl;
import org.activiti.engine.impl.persistence.entity.data.AbstractDataManager;
import org.activiti.engine.impl.persistence.entity.data.JobDataManager;
import org.activiti.engine.impl.persistence.entity.data.impl.cachematcher.JobsByExecutionIdMatcher;
import org.activiti.engine.runtime.Job;

public class MybatisJobDataManager extends AbstractDataManager<JobEntity> implements JobDataManager {
  protected CachedEntityMatcher<JobEntity> jobsByExecutionIdMatcher = new JobsByExecutionIdMatcher();

  public MybatisJobDataManager(ProcessEngineConfigurationImpl processEngineConfiguration) {
    super(processEngineConfiguration);
  }

  public Class<? extends JobEntity> getManagedEntityClass() {
    return JobEntityImpl.class;
  }

  public JobEntity create() {
    return new JobEntityImpl();
  }

  public List<JobEntity> findJobsToExecute(Page page) {
    return this.getDbSqlSession().selectList("selectJobsToExecute", (ListQueryParameterObject)null, page);
  }

  public List<JobEntity> findJobsByExecutionId(String executionId) {
    return this.getList("selectJobsByExecutionId", executionId, this.jobsByExecutionIdMatcher, true);
  }

  public List<JobEntity> findJobsByProcessDefinitionId(String processDefinitionId) {
    Map<String, String> params = new HashMap(1);
    params.put("processDefinitionId", processDefinitionId);
    return this.getDbSqlSession().selectList("selectJobByProcessDefinitionId", params);
  }

  public List<JobEntity> findJobsByTypeAndProcessDefinitionId(String jobHandlerType, String processDefinitionId) {
    Map<String, String> params = new HashMap(2);
    params.put("handlerType", jobHandlerType);
    params.put("processDefinitionId", processDefinitionId);
    return this.getDbSqlSession().selectList("selectJobByTypeAndProcessDefinitionId", params);
  }

  public List<JobEntity> findJobsByProcessInstanceId(String processInstanceId) {
    return this.getDbSqlSession().selectList("selectJobsByProcessInstanceId", processInstanceId);
  }

  public List<JobEntity> findExpiredJobs(Page page) {
    Date now = this.getClock().getCurrentTime();
    return this.getDbSqlSession().selectList("selectExpiredJobs", now, page);
  }

  public List<Job> findJobsByQueryCriteria(JobQueryImpl jobQuery, Page page) {
    String query = "selectJobByQueryCriteria";
    return this.getDbSqlSession().selectList("selectJobByQueryCriteria", jobQuery, page);
  }

  public long findJobCountByQueryCriteria(JobQueryImpl jobQuery) {
    return (Long)this.getDbSqlSession().selectOne("selectJobCountByQueryCriteria", jobQuery);
  }

  public void updateJobTenantIdForDeployment(String deploymentId, String newTenantId) {
    HashMap<String, Object> params = new HashMap();
    params.put("deploymentId", deploymentId);
    params.put("tenantId", newTenantId);
    this.getDbSqlSession().update("updateJobTenantIdForDeployment", params);
  }

  public void resetExpiredJob(String jobId) {
    Map<String, Object> params = new HashMap(2);
    params.put("id", jobId);
    this.getDbSqlSession().update("resetExpiredJob", params);
  }
}
